package com.yuandengta.yomall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yuandengta.yomall.product.dao.CategoryBrandRelationDao;
import com.yuandengta.yomall.product.entity.CategoryBrandRelationEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandengta.common.utils.PageUtils;
import com.yuandengta.common.utils.Query;

import com.yuandengta.yomall.product.dao.BrandDao;
import com.yuandengta.yomall.product.entity.BrandEntity;
import com.yuandengta.yomall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    /**
     * 品牌查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        // 从params中获取查询条件key
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.eq("brand_id", key).or().like("name", key);
        }

        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 级联更新品牌
     * @param brand
     */
    @CacheEvict(value = {"brand"}, allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCascade(BrandEntity brand) {
        // 保证数据表冗余字段的数据一致
        // 更新品牌表的数据
        this.updateById(brand);

        if (!StringUtils.isEmpty(brand.getName())) {
            // 同步更新品牌分类关联表中的数据
            CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
            categoryBrandRelationEntity.setBrandId(brand.getBrandId());
            categoryBrandRelationEntity.setBrandName(brand.getName());
            categoryBrandRelationDao.update(categoryBrandRelationEntity,
                    new UpdateWrapper<CategoryBrandRelationEntity>()
                            .eq("brand_id", brand.getBrandId()));

            // TODO 更新其他关联
        }
    }

    /**
     * 查询品牌列表
     * @param brandIds 品牌id集合
     * @return
     */
    @Cacheable(value = {"brand"}, key = "'getBrandsByIds:'+#root.args[0]")
    @Override
    public List<BrandEntity> getBrandsByIds(List<Long> brandIds) {
        List<BrandEntity> brandEntities = listByIds(brandIds);
        return brandEntities;
    }

}